﻿using System.Configuration;
using System.Data.Cassandra.Configuration;
using System.Data.Cassandra.Connection;
using System.Data.Cassandra.Connection.Pool;

namespace System.Data.Cassandra
{
    public class CassandraFactory
    {
        private static ConnectionPool pool;

        public CassandraFactory(String keyspace)
        {
            CassandraConfigurationSection cassandraConfigurationSection = GetConfigurationSection();
            CassandraConnectionConfig config = new CassandraConnectionConfig(cassandraConfigurationSection.Servers.ToList(), cassandraConfigurationSection.Port, keyspace, cassandraConfigurationSection.Timeout, cassandraConfigurationSection.MinPoolCountPerHost, cassandraConfigurationSection.MaxPoolCountPerHost);
            pool = new ConnectionPool(config);
        }

        public CassandraConnection GetConnection()
        {
            return new CassandraConnection(pool);
        }

        private CassandraConfigurationSection GetConfigurationSection()
        {
            Object section = ConfigurationManager.GetSection("cassandra");
            return section != null ? (CassandraConfigurationSection) section : new CassandraConfigurationSection();
        }
    }
}